package com.weitu.environmental.solidWaste.controller;


import com.weitu.base.annotaion.SystemLog;
import com.weitu.base.controller.BaseController;
import com.weitu.environmental.solidWaste.entity.SolidWasteApply;
import com.weitu.environmental.solidWaste.service.SolidWasteApplyService;
import com.weitu.paging.DataGridPagination;
import com.weitu.permission.user.entity.User;
import com.weitu.plugin.PageView;
import com.weitu.util.AjaxResult;
import com.weitu.workflow.definition.constant.FlowDefConstants;
import com.weitu.workflow.demo.entity.LeaveApplication;
import com.weitu.workflow.flowBusiness.constant.FlowBusinessConstants;
import com.weitu.workflow.flowManagement.entity.WorkflowReview;
import com.weitu.workflow.flowManagement.service.FlowManagementService;
import org.fireflow.engine.IProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangqingxi 2017-03-31
 */
@Controller
@RequestMapping("environmental/solidWaste/apply")
public class SolidWasteApplyController extends BaseController {

    private static final String MODULE_BATH_PATH = BASE_JSP_PATH + "environmental/solidWaste/apply";

    private final SolidWasteApplyService solidWasteApplyService;
    private final FlowManagementService flowManagementService;//流程管理service

    @Autowired
    public SolidWasteApplyController(SolidWasteApplyService solidWasteApplyService, FlowManagementService flowManagementService) {
        this.solidWasteApplyService = solidWasteApplyService;
        this.flowManagementService = flowManagementService;
    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listView() {

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName(MODULE_BATH_PATH + "/list");

        return modelAndView;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addView() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/add");
        return modelAndView;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editView(String id) {

        SolidWasteApply solidWasteApply = solidWasteApplyService.findById(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");
        modelAndView.addObject("apple", solidWasteApply);

        return modelAndView;
    }
    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public ModelAndView detailsView(String id) {

        if(null==id){
            Map<String, Object> paramMap = new HashMap<String, Object>();
            //获取参数
            putParametersIntoMap(paramMap);
            id=paramMap.get("businessId").toString();
        }

        SolidWasteApply solidWasteApply = solidWasteApplyService.findById(id);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(MODULE_BATH_PATH + "/details");
        modelAndView.addObject("apple", solidWasteApply);

        return modelAndView;
    }
    @RequestMapping(value = "/verify", method = RequestMethod.GET)
    public ModelAndView verifyView() {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取参数
        putParametersIntoMap(paramMap);
        //工作项id
        String workItemId=paramMap.get("workItemId").toString();
        String businessId=paramMap.get("businessId").toString();
        //表单类型；0：只读表单；1：编辑表单
        int formType=paramMap.get("formType")!=null?Integer.parseInt(paramMap.get("formType").toString()):0;
        modelAndView.addObject("workItemId",workItemId);
        modelAndView.addObject("formType",formType);
        //获取意见信息
        WorkflowReview workflowReview=this.flowManagementService.queryWorkflowReviewByWorkItemId(workItemId);
        modelAndView.addObject("workflowReview",workflowReview);

        SolidWasteApply solidWasteApply = solidWasteApplyService.findById(businessId);


        modelAndView.setViewName(MODULE_BATH_PATH + "/details");
        modelAndView.addObject("apple", solidWasteApply);

        return modelAndView;
    }

    @RequestMapping(value = "/write", method = RequestMethod.GET)
    public ModelAndView writeView() {
        ModelAndView modelAndView = new ModelAndView();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取参数
        putParametersIntoMap(paramMap);
        //工作项id
        String workItemId=paramMap.get("workItemId").toString();
        String businessId=paramMap.get("businessId").toString();
        //表单类型；0：只读表单；1：编辑表单
        int formType=paramMap.get("formType")!=null?Integer.parseInt(paramMap.get("formType").toString()):0;
        modelAndView.addObject("workItemId",workItemId);
        modelAndView.addObject("formType",formType);
        //获取意见信息
        WorkflowReview workflowReview=this.flowManagementService.queryWorkflowReviewByWorkItemId(workItemId);
        modelAndView.addObject("workflowReview",workflowReview);

        SolidWasteApply solidWasteApply = solidWasteApplyService.findById(businessId);


        modelAndView.setViewName(MODULE_BATH_PATH + "/edit");
        modelAndView.addObject("apple", solidWasteApply);

        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/verify", method = RequestMethod.POST)
    @SystemLog(module = "环保管理_固废管理_固废申请", methods = "审批结束")
    public AjaxResult verifyView(HttpServletRequest request)throws Exception {

        String workItemId=request.getParameter("workItemId");//获取工作项id
        //查询处理结果及意见
        WorkflowReview workflowReview=this.flowManagementService.getWorkflowReviewByRequest(request,"workflowReview",workItemId);
        //保存处理结果
        boolean boo=this.flowManagementService.saveWorkflowReview(workflowReview);

        //结束环节，继续流程向下执行
        if (boo){
            flowManagementService.workItemComplete(workItemId);
        }
        System.out.println(workflowReview.getComments());

        return updateById();
    }

    @ResponseBody
    @RequestMapping("/findByParam")
    public Object findByParam(HttpServletRequest request) {


        Map<String, Object> paramMap = new HashMap<String, Object>();

        //获取前台参数
        putParametersIntoMap(paramMap);

        Integer totalNum = solidWasteApplyService.findByParamCount(paramMap);//获取总数
        DataGridPagination pagination = DataGridPagination.getInstance(request, totalNum);

        PageView pageView = pagination.getPageView();//生成pageView分页对象
        paramMap.put("pageView", pageView);

        List<SolidWasteApply> solidWasteApplyList = solidWasteApplyService.findByParam(paramMap);

        return pagination.flushDataGridData(solidWasteApplyList);
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @SystemLog(module = "环保管理_固废管理_固废申请", methods = "添加固废申请")
    public AjaxResult add() {


        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(paramMap);

        Integer result = solidWasteApplyService.add(paramMap);

        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @SystemLog(module = "环保管理_固废管理_固废申请", methods = "编辑固废申请")
    public AjaxResult edit() {

        return updateById();
    }
    @ResponseBody
    @RequestMapping(value = "/editSubmit", method = RequestMethod.POST)
    @SystemLog(module = "环保管理_固废管理_固废申请", methods = "提交固废申请")
    public AjaxResult editSubmit() throws Exception{

        User loginUser=getSessionUser();

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);

        String id=paramMap.get("id").toString();//id
        //设置流程变量
        Map<String,String> dataMap=new HashMap<String, String>();
        String nextUserId=paramMap.get("nextUserId").toString();//第一环节处理人
        dataMap.put(FlowDefConstants.FLOW_FIRSTUSER_ID,nextUserId);
        String content="固废申请审批流程";
        //启动流程实例
        flowManagementService.flowCreateAndRun(FlowBusinessConstants.BUSINESS_SOLIDWASTEAPPLY,id,content,loginUser.getName(),dataMap);

        return updateById();
    }
    @ResponseBody
    @RequestMapping(value = "/addSubmit", method = RequestMethod.POST)
    @SystemLog(module = "环保管理_固废管理_固废申请", methods = "提交固废申请")
    public AjaxResult addSubmit() throws Exception{

        User loginUser=getSessionUser();

        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getAddBasicFieldMap(paramMap);

        String id=paramMap.get("id").toString();//id
        //设置流程变量
        Map<String,String> dataMap=new HashMap<String, String>();
        String nextUserId=paramMap.get("nextUserId").toString();//第一环节处理人
        dataMap.put(FlowDefConstants.FLOW_FIRSTUSER_ID,nextUserId);
        String content="固废申请审批流程";
        //启动流程实例
        flowManagementService.flowCreateAndRun(FlowBusinessConstants.BUSINESS_SOLIDWASTEAPPLY,id,content,loginUser.getName(),dataMap);

        Integer result = solidWasteApplyService.add(paramMap);

        return resultAjaxResult(result);
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @SystemLog(module = "环保管理_固废管理_固废申请", methods = "删除固废申请")
    public AjaxResult delete() {

        return updateById();
    }

    /**
     * 修改的方法
     *
     * @return 处理结果
     */
    public AjaxResult updateById() {


        Map<String, Object> paramMap = new HashMap<String, Object>();
        //获取前台参数
        getEditBasicFieldMap(paramMap);

        Integer result = solidWasteApplyService.updateById(paramMap);

        return resultAjaxResult(result);
    }


    /**
     * 处理返回参数
     *
     * @param result 返回值
     * @return 返回值
     */
    private AjaxResult resultAjaxResult(Integer result) {
        AjaxResult ajaxResult = new AjaxResult();
        if (result > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("操作成功");
        }
        return ajaxResult;
    }


}
